package com.pay.controller;

import com.pay.pojo.PayInfo;
import com.pay.service.impl.PayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    PayServiceImpl payService;

    @GetMapping("/create")
    public ModelAndView create(@RequestParam String orderId,@RequestParam Double amount) {
        Map map = new HashMap<>();
        map.put("codeUrl", payService.create(orderId, BigDecimal.valueOf(amount)).getCodeUrl());
        return new ModelAndView("create", map);
    }

    @PostMapping("/notify")
    @ResponseBody
    public String asyncNotify(@RequestBody String notifyData){
        String result = payService.asyncNotify(notifyData);
        return result;
    }

    @GetMapping("/queryByOrderId")
    @ResponseBody
    public PayInfo queryByOrderId(@RequestParam String orderId){
        return payService.queryByOrderId(orderId);
    }
}
